from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.shortcuts import redirect
from common.models import User, Complain, Result

# Create your views here.

def list(request):
    username = request.session.get('username', '游客')
    user = User.objects.filter(username=username)
    if user and user[0].type:
        complainList = Complain.objects.all()
        return render(request, 'staff/list.html', {'complains': complainList, 'username': username})
    else:
        return redirect('/common/login')

def detail(request):
    username = request.session.get('username', '游客')
    title = request.GET.get('title')
    complain = Complain.objects.get(title=title)
    content = complain.content
    title = complain.title
    try:
        result = complain.result
        return render(request, 'staff/detail.html', {'complain': complain, 'result': result.content, 'username': username})
    except:
        return render(request, 'staff/detail.html', {'complain': complain, 'result': '暂无处理结果', 'username': username})

def showdetail(request):
    # 获取信息
    username = request.session.get('username')
    user = User.objects.get(username=username)
    content = request.POST.get('result')
    title = request.POST.get('title')
    complain = Complain.objects.get(title=title)
    # 新建或修改
    try:
        record = Result.objects.create(user=user, content=content, complain=complain)
    except:
        result = Result.objects.get(user=user, complain=complain)
        result.content = content
        result.save()

    return redirect("/staff/list")



